package com.tduck.cloud.report.data.db;

import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.EntityListHandler;
import cn.hutool.db.sql.SqlExecutor;
import com.tduck.cloud.report.data.entity.DataSourceConfig;
import lombok.experimental.UtilityClass;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * 报表数据源工厂
 */
@UtilityClass
public class ReportJdbcTemplate {


    /**
     * 执行sql
     *
     * @param dataSourceConfig 数据源配置
     * @param sql              sql
     * @return 结果集
     */
    public List<Entity> execute(DataSourceConfig dataSourceConfig, String sql) {
        Connection conn = null;
        try {
            conn = ReportDataConnectionFactory.getJdbcConnectionPool(dataSourceConfig).getConnection();
            List<Entity> entityList = SqlExecutor.query(conn, sql, new EntityListHandler());
            return entityList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtil.close(conn);
        }
    }



}
